package com.funtonow.jqdk.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.funtonow.jqdk.R;
import com.funtonow.jqdk.adapter.LoanListAdapter;
import com.funtonow.jqdk.base.BaseActivity;
import com.funtonow.jqdk.base.BaseBean;
import com.funtonow.jqdk.base.Constants;
import com.funtonow.jqdk.base.SqjAppliaction;
import com.funtonow.jqdk.base.UrlUtils;
import com.funtonow.jqdk.bean.DkNumBean;
import com.funtonow.jqdk.bean.GameThirdInfoListBean;
import com.funtonow.jqdk.bean.LoanInfoBean;
import com.funtonow.jqdk.bean.UserInfoBean;
import com.funtonow.jqdk.utils.GlideUtils;
import com.funtonow.jqdk.utils.HttpUtil;
import com.funtonow.jqdk.utils.SPUtil;
import com.funtonow.jqdk.utils.StatisticsUtils;
import com.funtonow.jqdk.utils.ToastUtils;
import com.shehuan.niv.NiceImageView;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

import static com.funtonow.jqdk.base.UrlUtils.TUI_JIAN;

/**
 * @author Jiajien
 * @date 2019/6/26
 * 审核成功
 */
public class ExamineSuccessActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.rlv_recommend)
    RecyclerView rlvRecommend;
    @BindView(R.id.ll_recommend)
    LinearLayout llRecommend;
    @BindView(R.id.tv_recommend_loan_max)
    TextView tvRecommedLoanMax;
    @BindView(R.id.tv_recommend_loan_name)
    TextView tvRecommedLoanName;
    @BindView(R.id.tv_recommend_loan_lv)
    TextView tvRecommedLoanLv;
    @BindView(R.id.iv_recommend_loan_img)
    NiceImageView ivRecommedLoanImg;


    private LoanListAdapter recommendAdapter;
    private List<GameThirdInfoListBean> gameThirdInfoListBeans = new ArrayList<>();
    private GameThirdInfoListBean gameThirdInfoListBeanFirst;  // list 第一条


    @Override
    public int getLayoutRes() {
        return R.layout.activity_examine_success;
    }

    @Override
    public void onCreateActivity(@Nullable Bundle savedInstanceState) {
        initToolbar(getResources().getString(R.string.title_user_info));
        initRecyclerView(rlvRecommend);
        getAppId();
        recommendAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (SPUtil.getUserInfo() != null && recommendAdapter.getItem(position).getVipfw() == 1
                        && SPUtil.getUserInfo().getHasVip() == 0) {
                    ToastUtils.show("该产品仅限会员申请");
                    startActivity(new Intent(ExamineSuccessActivity.this, VipActivity.class));
                } else {
                    GameThirdInfoListBean gameThirdInfoListBean = recommendAdapter.getItem(position);
                    assert gameThirdInfoListBean != null;
                    StatisticsUtils.clickUrl(gameThirdInfoListBean);
                    StatisticsUtils.intentWebView(ExamineSuccessActivity.this,
                            gameThirdInfoListBean.getUrl(), gameThirdInfoListBean.getName(),
                            gameThirdInfoListBean.getChanpinid());
                }
                finish();
            }
        });
    }

    private void initRecyclerView(RecyclerView recyclerView) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        //设置增加或删除条目的动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recommendAdapter = new LoanListAdapter();
        recyclerView.setAdapter(recommendAdapter);
    }

    /**
     * 推荐列表
     */
    private void recommend() {
        UserInfoBean userInfoBean = SPUtil.getUserInfo();
        Map<String, String> param = new HashMap<>();
        assert userInfoBean != null;
        param.put("appid", Constants.OWN_APP_ID);
        param.put("token", userInfoBean.getToken());
        param.put("openid", userInfoBean.getOpenid());
        param.put("showappid", Constants.DATA_APP_ID);
        HttpUtil.getInstance().postDataAsynToNet(TUI_JIAN, param, new HttpUtil.MyNetCall() {
            @Override
            public void success(Call call, String jsonString) throws IOException {
                String dkAppId = (String) SPUtil.get("dkAppId", "");

                JSONObject jsonObject = JSON.parseObject(SqjAppliaction.loanInfoBean.getCpMap());
                String jsonArray = JSON.parseObject(jsonString).getJSONArray("data").toJSONString();
                List<String> stringList = JSON.parseArray(jsonArray, String.class);
                for (String s : stringList) {
                    for (LoanInfoBean.HeziInfoListBean heziInfoListBean : SqjAppliaction.loanInfoBean.getHeziInfoList()) {
                        if (s.equals(heziInfoListBean.getGdid())) {
                            for (LoanInfoBean.GdlistBean gdlistBean : SqjAppliaction.loanInfoBean.getGdlist()) {
                                if (s.equals(gdlistBean.getGdid())) {
                                    GameThirdInfoListBean gameThirdInfoListBean = JSON.parseObject(jsonObject.getJSONObject(gdlistBean.getChanpinid()).toJSONString(), GameThirdInfoListBean.class);
                                    gameThirdInfoListBean.setVipfw(gdlistBean.getVipfw());
                                    gameThirdInfoListBean.setUrl(gdlistBean.getUrl());
                                    gameThirdInfoListBean.setHeziId(heziInfoListBean.getType());
                                    gameThirdInfoListBean.setIndex(heziInfoListBean.getInde());
                                    gameThirdInfoListBean.setGdId(s);
                                    gameThirdInfoListBeans.add(gameThirdInfoListBean);
                                }
                                if (Objects.equals(dkAppId, gdlistBean.getChanpinid())) {
                                    gameThirdInfoListBeanFirst = JSON.parseObject(jsonObject.getJSONObject(dkAppId).toJSONString(), GameThirdInfoListBean.class);
                                    gameThirdInfoListBeanFirst.setHeziId(heziInfoListBean.getType());
                                    gameThirdInfoListBeanFirst.setIndex(heziInfoListBean.getInde());
                                    gameThirdInfoListBeanFirst.setGdId(s);
                                    BigDecimal money = new BigDecimal(gameThirdInfoListBeanFirst.getEdumax());
                                    NumberFormat format = new DecimalFormat("###,###");
                                    tvRecommedLoanMax.setText(String.format(getResources().getString(R.string.rmb),
                                            format.format(money)));
                                    tvRecommedLoanName.setText(gameThirdInfoListBeanFirst.getName());
                                    tvRecommedLoanLv.setText(String.format("%s | 期限%s", gameThirdInfoListBeanFirst.getFeilv(), gameThirdInfoListBeanFirst.getWeixin()));
                                    GlideUtils.setHeadImg(ivRecommedLoanImg, gameThirdInfoListBeanFirst.getIcon());
                                }
                            }
                        }
                    }
                }
                if (gameThirdInfoListBeans.size() > 0) {
                    llRecommend.setVisibility(View.VISIBLE);
                    recommendAdapter.setNewData(gameThirdInfoListBeans);
                } else {
                    llRecommend.setVisibility(View.GONE);
                }
                StatisticsUtils.baoGuang(Constants.HEZI_ID_25);
            }

            @Override
            public void failed(Call call, IOException e) {

            }
        });
    }


    @OnClick({R.id.iv_examine_sub})
    @Override
    public void onClick(View v) {
        StatisticsUtils.clickUrl(gameThirdInfoListBeanFirst);
        StatisticsUtils.intentWebView(ExamineSuccessActivity.this,
                gameThirdInfoListBeanFirst.getUrl(), gameThirdInfoListBeanFirst.getName(),
                gameThirdInfoListBeanFirst.getChanpinid());
    }

    /**
     * 获取数据appId
     */
    private void getAppId() {
        HttpUtil.getInstance().getDataAsynFromNet(UrlUtils.GET_APPID, new HttpUtil.MyNetCall() {
            @Override
            public void success(Call call, String jsonString) throws IOException {
                Constants.DATA_APP_ID = JSON.parseObject(jsonString).getString("appId");
                recommend();
            }

            @Override
            public void failed(Call call, IOException e) {

            }
        });
    }



}
